//
//  WBBaseViewController.swift
//  weibo
//
//  Created by 李海超 on 2017/5/21.
//  Copyright © 2017年 lihaichao. All rights reserved.
//

















import UIKit

class WBBaseViewController: UIViewController {
    
    //访客视图字典信息
    var visitorDict:[String:String]?
    //用户登录标记
    var userLogon = true
    //tableview
    var tableView : UITableView?
    //下拉刷新
    var refreshControl:UIRefreshControl?
    //判断上拉刷新标记
    var isPullUp = false
    
    
    
    //自定义导航条
    lazy var navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    
    //自定义导航内容项
    lazy var navItem = UINavigationItem()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    /// 监测title属性被设置
    override var title: String?{

        didSet{
            
            navItem.title = title

        }
        
    }
    
    //加载数据
    func loadData() {
        //如果子类不实现任何方法，就默认关闭刷新控件
        refreshControl?.endRefreshing()
    }
    
    
   
// MARK: - 创建UI

   private func setupUI(){
        
        //取消自动缩进
        automaticallyAdjustsScrollViewInsets = false
        
        view.backgroundColor = UIColor.white
        
        setupNavigationUI()
        
        userLogon ? setupTaleView():setupVistorView()
        
    }
    
    /**
     创建访客UIView
     */
    private func setupVistorView(){
    
    
        let visitorView = WBVisitorView(frame: view.bounds)
                
        view.insertSubview(visitorView, belowSubview: navBar)
        
        visitorView.infoDict = visitorDict
        
        visitorView.loginButton.addTarget(self, action: #selector(loginButton), for: .touchUpInside)
        visitorView.registerButton.addTarget(self, action: #selector(registerButton), for: .touchUpInside)
        
        //使用系统的构造函数
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerButton))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录",style: .plain, target: self, action: #selector(loginButton))
        
        
    }
    
    @objc private func loginButton(){
                
        print("用户登录")
    }
    
    @objc private func registerButton(){
        print("用户注册")
    }
    
    //设置tableView的UI
    
    func setupTaleView(){
    
        tableView = UITableView(frame: view.bounds, style: .plain)
        
        view.insertSubview(tableView!, belowSubview: navBar)
        
        tableView?.dataSource = self
        tableView?.delegate = self
        
        tableView?.contentInset = UIEdgeInsets(top: navBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        
        //实例化下拉刷新控件
        refreshControl = UIRefreshControl()
        
        tableView?.addSubview(refreshControl!)
        
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        
    }
    
    //设置导航栏UI
    private func setupNavigationUI(){
        
        //添加导航条
        view.addSubview(navBar)
        
        //给自定义navigationBar指定navigationItem
        
        navBar.items = [navItem]
        
        //bar的背景颜色
        navBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        //标题颜色
        navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        //item文字颜色
        navBar.tintColor = UIColor.orange
        
    }
}

// MARK: - tableview代理方法
extension WBBaseViewController:UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    //将要显示cell，在现实最后一行时，刷新数据
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        //取出最大一行的下标
        let maxSection = tableView.numberOfSections - 1
        
        let maxRow = tableView.numberOfRows(inSection: maxSection)
        
        //判断当前行和最大行的大小，并进行刷新
        if indexPath.section == maxSection && indexPath.row == (maxRow-1) && !isPullUp {
            
            print("上拉刷新")
            
            isPullUp = true
            
            loadData()
        }

        
    }
    
}
